package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.*;
import com.atguigu.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author lhh
 * @ClassName HouseController
 * @date 2022/9/18 10:57
 */

@Controller
@RequestMapping("house")
public class HouseController extends BaseController{

    @Reference
    private HouseService houseService;

    @Reference
    private CommunityService communityService;

    @Reference
    private DictService dictService;

    @Reference
    private HouseBrokerService houseBrokerService;

    @Reference
    private HouseImageService houseImageService;

    @Reference
    private HouseUserService houseUserService;

    @RequestMapping
    public String index(HttpServletRequest request, Model model){
        Map<String, Object> filters = getFilters(request);
        PageInfo<House> page = houseService.findPage(filters);
        model.addAttribute("page",page);
        model.addAttribute("filters",filters);
        this.getList(model);
        return "house/index";
    }

    @GetMapping("create")
    public String create(Model model){
        this.getList(model);
        return "house/create";
    }

    @PostMapping("save")
    public String save(House house){
        houseService.save(house);
        return "common/successPage";
    }

    @GetMapping("edit/{id}")
    public String edit(@PathVariable Long id,Model model){
        House house = houseService.getById(id);
        this.getList(model);
        model.addAttribute("house",house);
        return "house/edit";
    }

    @PostMapping("update")
    public String update(House house){
        houseService.update(house);
        return "common/successPage";
    }

    @GetMapping("delete/{id}")
    public String delete(@PathVariable Long id){
        houseService.remove(id);
        return "redirect:/house";
    }

    @GetMapping("publish/{id}/{status}")
    public String updateStatus(@PathVariable Long id,@PathVariable Integer status){
        houseService.publish(id, status);
        return "redirect:/house";
    }

    @GetMapping("{id}")

    public String getDetails(@PathVariable Long id,Model model){

        //房源信息
        House house = houseService.getById(id);
        model.addAttribute("house",house);

        //小区信息
        Community community = communityService.getById(house.getCommunityId());
        model.addAttribute("community",community);

        //经纪人信息
        List<HouseBroker> houseBrokerList=houseBrokerService.findListByHouseId(id);
        model.addAttribute("houseBrokerList",houseBrokerList);

        //房东信息
        List<HouseUser> houseUserList=houseUserService.findListByHouseId(id);
        model.addAttribute("houseUserList",houseUserList);

        //房源图片
        List<HouseImage> houseImage1List=houseImageService.findList(id,1);
        model.addAttribute("houseImage1List",houseImage1List);

        //房产图片
        List<HouseImage> houseImage2List=houseImageService.findList(id,2);
        model.addAttribute("houseImage2List",houseImage2List);

        return "house/show";
    }

    /**
     * 封装根据dictCode编码获取不同的编码列表
     */
    protected Model getList(Model model){

        model.addAttribute("communityList",communityService.findAll());

        model.addAttribute("houseTypeList",dictService.findListByDictCode("houseType"));

        model.addAttribute("floorList",dictService.findListByDictCode("floor"));

        model.addAttribute("buildStructureList",dictService.findListByDictCode("buildStructure"));

        model.addAttribute("directionList",dictService.findListByDictCode("direction"));

        model.addAttribute("decorationList",dictService.findListByDictCode("decoration"));

        model.addAttribute("houseUseList",dictService.findListByDictCode("houseUse"));

        return model;
    }
}
